﻿using System;
namespace Producers.Kis.Xtee.Riik.Ee.Producer.Kis {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="RepresentativeData", Namespace="http://producers.kis.xtee.riik.ee/producer/kis", Target=typeof(RepresentativeData))]
    public class RepresentativeDataSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IRepresentativeData clazz;
        
        public RepresentativeDataSerializer(IRepresentativeData clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("RepresentativePerson");
            if (clazz.RepresentativePerson!=null) {
                global::Xtee.Core.SerializationMetadata metaRepresentativePerson = this.GetMetaData(clazz.RepresentativePerson);
                Writer.WriteTypeAttribute(metaRepresentativePerson.TypeQualifiedName.Namespace, metaRepresentativePerson.TypeQualifiedName.Name);
                metaRepresentativePerson.NewSerializer(clazz.RepresentativePerson).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : RepresentativePerson");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("BasicData");
            if (clazz.BasicData!=null) {
                global::Xtee.Core.SerializationMetadata metaBasicData = this.GetMetaData(clazz.BasicData);
                Writer.WriteTypeAttribute(metaBasicData.TypeQualifiedName.Namespace, metaBasicData.TypeQualifiedName.Name);
                metaBasicData.NewSerializer(clazz.BasicData).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : BasicData");
            }
            Writer.WriteEndElement();
            if (clazz.Title!=null) {
                Writer.WriteStartElement("Title");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Title))).ToString());
                Writer.WriteEndElement();
            }
            Writer.WriteStartElement("IsSender");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "boolean");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdBoolean(((bool)(clazz.IsSender))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("RepresentationRight");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "boolean");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdBoolean(((bool)(clazz.RepresentationRight))).ToString());
            Writer.WriteEndElement();
            for (int x = 0; ((clazz.ContactInformation != null) 
                        && (x < clazz.ContactInformation.Count)); x = (x + 1)) {
                if (clazz.ContactInformation[x]!=null) {
                    Writer.WriteStartElement("ContactInformation");
                    global::Xtee.Core.SerializationMetadata metaContactInformation = this.GetMetaData(clazz.ContactInformation[x]);
                    Writer.WriteTypeAttribute(metaContactInformation.TypeQualifiedName.Namespace, metaContactInformation.TypeQualifiedName.Name);
                    metaContactInformation.NewSerializer(clazz.ContactInformation[x]).WriteXml(Writer);
                    Writer.WriteEndElement();
                }
            }
            Writer.WriteStartElement("Address");
            if (clazz.Address!=null) {
                global::Xtee.Core.SerializationMetadata metaAddress = this.GetMetaData(clazz.Address);
                Writer.WriteTypeAttribute(metaAddress.TypeQualifiedName.Namespace, metaAddress.TypeQualifiedName.Name);
                metaAddress.NewSerializer(clazz.Address).WriteXml(Writer);
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Address");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="RepresentativePerson") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeData>RepresentativePerson");
                }
                global::Xtee.Core.SerializationMetadata metaRepresentativePerson = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IRepresentativePerson>(Reader, "RepresentativePerson", "http://producers.kis.xtee.riik.ee/producer/kis");
                clazz.RepresentativePerson = metaRepresentativePerson.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IRepresentativePerson>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("RepresentativePerson");
                    metaRepresentativePerson.NewSerializer(clazz.RepresentativePerson).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeData>RepresentativePerson", Reader.LocalName);
            }
            if (Reader.LocalName=="BasicData") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeData>BasicData");
                }
                global::Xtee.Core.SerializationMetadata metaBasicData = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IRepresentationBasicData>(Reader, "RepresentationBasicData", "http://producers.kis.xtee.riik.ee/producer/kis");
                clazz.BasicData = metaBasicData.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IRepresentationBasicData>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("BasicData");
                    metaBasicData.NewSerializer(clazz.BasicData).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeData>BasicData", Reader.LocalName);
            }
            if (Reader.LocalName=="Title") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeData>Title");
                }
                clazz.Title = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("Title")).Value;
            }
            if (Reader.LocalName=="IsSender") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeData>IsSender");
                }
                clazz.IsSender = new global::Xtee.Core.Schema.XsdBoolean(Reader.ReadElementString("IsSender")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeData>IsSender", Reader.LocalName);
            }
            if (Reader.LocalName=="RepresentationRight") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeData>RepresentationRight");
                }
                clazz.RepresentationRight = new global::Xtee.Core.Schema.XsdBoolean(Reader.ReadElementString("RepresentationRight")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeData>RepresentationRight", Reader.LocalName);
            }
            if (Reader.LocalName=="ContactInformation") {
                clazz.ContactInformation = new System.Collections.Generic.List<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IContactInformation>();
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "ContactInformation")); ) {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeData>ContactInformation");
                    }
                    global::Xtee.Core.SerializationMetadata metaContactInformation = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IContactInformation>(Reader, "ContactInformation", "http://producers.kis.xtee.riik.ee/producer/kis");
                    global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IContactInformation item = metaContactInformation.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IContactInformation>();
                    clazz.ContactInformation.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("ContactInformation");
                        metaContactInformation.NewSerializer(item).ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
            }
            if (Reader.LocalName=="Address") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>RepresentativeData>Address");
                }
                global::Xtee.Core.SerializationMetadata metaAddress = this.GetMetaData<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IAddress>(Reader, "Address", "http://producers.kis.xtee.riik.ee/producer/kis");
                clazz.Address = metaAddress.NewEntity<global::Producers.Kis.Xtee.Riik.Ee.Producer.Kis.IAddress>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("Address");
                    metaAddress.NewSerializer(clazz.Address).ReadXml(Reader);
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>RepresentativeData>Address", Reader.LocalName);
            }
        }
    }
}
